package utils

import (
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/utils/enum"
	"fmt"

	"github.com/gogf/gf/crypto/gmd5"
	"github.com/gogf/gf/frame/g"
)

func DoubleMd5(pwd string) string {
	md5Pwd, err := gmd5.EncryptString(pwd)
	if err != nil {
		defErr.Panic(define.EncryptFail)
	}
	var encryptVal string

	if encryptVal, err = gmd5.EncryptString(md5Pwd + enum.PasswordSalt); err != nil {
		defErr.Panic(define.EncryptFail)
	}
	return encryptVal
}

func Md5Direct(pwd string) string {
	if encryptVal, err := gmd5.EncryptString(enum.PasswordSalt + pwd); err != nil {
		defErr.Panic(define.EncryptFail)
	} else {
		return encryptVal
	}
	return ""
}

//将字符串加密成 md5
func String2md5(str string) string {
	fmt.Println(str)
	md5Str, err := gmd5.EncryptString(str)
	if err != nil {
		g.Log().Errorf("将字符串加密成失败:", err)
		return ""
	}
	//data := []byte(str)
	//has := md5.Sum(data)
	//return fmt.Sprintf("%x", has) //将[]byte转成16进制
	return md5Str
}
